package com.ruoyi.businessOrder.service.impl;

import cn.elegent.pay.CallBackService;
import com.ruoyi.businessOrder.domain.BusinessOrder;
import com.ruoyi.businessOrder.service.IBusinessOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CallBackServiceImpl implements CallBackService {

    @Autowired
    private IBusinessOrderService businessOrderService;

    @Override
    public void successPay(String orderSn) {

        log.info("支付成功回调！"+orderSn);
        BusinessOrder order = businessOrderService.getByOrderCode(orderSn);
        if (order != null){
            if ("待支付".equals(order.getOrderStatus())) {
                // 更新订单状态
                order.setOrderStatus("100");
                order.setPayStatus("101");
                businessOrderService.updateBusinessOrder(order);
            };
        }
    }

    @Override
    public void failPay(String orderSn) {
        log.info("支付失败回调！"+orderSn);
    }

    @Override
    public void successRefund(String orderSn) {
        log.info("退款成功回调！"+orderSn);
        // 订单状态（1=已提交 2=处理中 3=已完成 4=已取消）
        // 支付状态（0=待支付 1=已支付 2=退款中 3=已退款）
        BusinessOrder order = businessOrderService.getByOrderCode(orderSn);
        if (order != null){
            order.setPayStatus("3");
            businessOrderService.updateBusinessOrder(order);
        }
    }

    @Override
    public void failRefund(String orderSn) {
        log.info("退款失败回调！"+orderSn);
        BusinessOrder order = businessOrderService.getByOrderCode(orderSn);
        if (order != null){
            order.setPayStatus("2");
            businessOrderService.updateBusinessOrder(order);
        }
    }

}
